package vn.keho.module.dompeto.model;

import com.google.gson.annotations.SerializedName;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by KeHo on 2/3/15.
 */
public class PageModel {
    @SerializedName("pageid")
    private int pageid;
    @SerializedName("title")
    private String title;
    @SerializedName("slots")
    private List<ChannelModel> slots;

    public PageModel(int pageid, String title) {
        this.pageid = pageid;
        this.title = title;
    }

    public PageModel(JSONObject jsonObject) {
        try {
            this.pageid = jsonObject.getInt("pageid");
            this.title = jsonObject.getString("title");

            this.slots = new ArrayList<ChannelModel>();
            JSONArray jsonArray = jsonObject.getJSONArray("slots");
            for(int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonChannel = jsonArray.getJSONObject(i);
                ChannelModel channelModel = new ChannelModel(jsonChannel);
                this.slots.add(channelModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public JSONObject toJSON() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("pageid", pageid);
            jsonObject.put("title", title);
            JSONArray jsonArray = new JSONArray();

            for(ChannelModel channelModel : slots) {
                jsonArray.put(channelModel.toJSON());
            }
            jsonObject.put("slots", jsonArray);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    public int getPageid() {
        return pageid;
    }

    public void setPageid(int pageid) {
        this.pageid = pageid;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<ChannelModel> getSlots() {
        return slots;
    }

    public void setSlots(List<ChannelModel> slots) {
        this.slots = slots;
    }
}
